# ========================================
# Clean Duplicate Icon Files (PNG)
# Keep only WEBP versions
# ========================================

$OutputEncoding = [System.Text.Encoding]::UTF8
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8

Write-Host ""
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "Clean Duplicate Icon Files" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""

$projectRoot = $PSScriptRoot
$resDir = Join-Path $projectRoot "app\src\main\res"

$iconNames = @(
    "ic_app_icon_debug",
    "ic_app_icon_beta",
    "ic_app_icon_stable"
)

$mipmapDirs = @(
    "mipmap-mdpi",
    "mipmap-hdpi",
    "mipmap-xhdpi",
    "mipmap-xxhdpi",
    "mipmap-xxxhdpi"
)

Write-Host "Searching for duplicate PNG files..." -ForegroundColor Yellow
Write-Host ""

$deletedCount = 0

foreach ($mipmapDir in $mipmapDirs) {
    $dirPath = Join-Path $resDir $mipmapDir
    
    if (-not (Test-Path $dirPath)) {
        continue
    }
    
    foreach ($iconName in $iconNames) {
        # Check for both regular and round versions
        $pngFile = Join-Path $dirPath "$iconName.png"
        $pngFileRound = Join-Path $dirPath "${iconName}_round.png"
        $webpFile = Join-Path $dirPath "$iconName.webp"
        $webpFileRound = Join-Path $dirPath "${iconName}_round.webp"
        
        # Delete PNG if WEBP exists
        if ((Test-Path $pngFile) -and (Test-Path $webpFile)) {
            Remove-Item $pngFile -Force
            Write-Host "  Deleted: $mipmapDir\$iconName.png" -ForegroundColor Green
            $deletedCount++
        }
        
        if ((Test-Path $pngFileRound) -and (Test-Path $webpFileRound)) {
            Remove-Item $pngFileRound -Force
            Write-Host "  Deleted: $mipmapDir\${iconName}_round.png" -ForegroundColor Green
            $deletedCount++
        }
    }
}

Write-Host ""
Write-Host "========================================" -ForegroundColor Green
Write-Host "Cleanup complete!" -ForegroundColor Green
Write-Host "Deleted $deletedCount duplicate PNG files" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Green
Write-Host ""
Write-Host "Next steps:" -ForegroundColor Yellow
Write-Host "  1. Build > Clean Project" -ForegroundColor White
Write-Host "  2. Build > Rebuild Project" -ForegroundColor White
Write-Host "  3. Run the app" -ForegroundColor White
Write-Host ""
Write-Host "Press any key to exit..." -ForegroundColor Gray
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown')

